package com.cn.ccp.action.course.select;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.ResourseBean;
import com.cn.ccp.bean.SpecialtyBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;
import com.cn.ccp.util.Contants;

/**
 * 学生选课action
 * @author renyongcheng
 *
 */
public class SelectCourseTableListAction extends BaseAction {
	private Logger log = Logger.getLogger(SelectCourseTableListAction.class);
	private String strStudentId;
	private String strCourseList;//所有课程列表字符串
	private String strSpecialty;//所有专业字符串
	private String strCourseName;//课程名称
	private String selectSpecialtyId,selectTeacherID,selectStarLevel,selectYear,selectCourseType,selectLanguage;//选中的专业id，教师id，星级，发布年份
	private List<SpecialtyBean> specailtyList;
	private List<UserBean> teacherList;
	private List<ReleaseTime> releaseList;
	private List<StartLevel> levelList;
	private List<ResourseBean> courseTypeList,languageList;
//	private boolean showTable = false;
	private List<CourseBean>  courseList;
	private String btnShowValue;
	
	public SelectCourseTableListAction(){
		log.info("----------SelectCourseAction()-------------------");
		UserBean userBean = CCPContants.getUserBean();
		strStudentId = String.valueOf(userBean.getId());
		pageSize = 10;
		log.info("----------SelectCourseAction()-------------------pageSize = "+pageSize);
		setPageSize(pageSize);
		initSpecialty();
		initTeachers();
		initReleaseTime();
		initLevel();
		//课程分类list
		courseTypeList = getResourseService().pageQueryResourse(Contants.DATA_DICTIONARY_CLASS_TYPE);
		//课程语言分类list
		languageList = getResourseService().pageQueryResourse(Contants.DATA_DICTIONARY_LANGUAGE_TYPE);
		getpagedetail();
	}

	public void changeList(){
		getpagedetail();
	}
	
	
	
	private void initSpecialty(){
		specailtyList  = getSelectCourseService().allSpecialty();
	}
	
	private void initTeachers(){
		teacherList = getSelectCourseService().allTeachers();
	}
	
	private void initReleaseTime(){
		releaseList = new ArrayList<ReleaseTime>();
		releaseList.add(new ReleaseTime("2013"));
		releaseList.add(new ReleaseTime("2014"));
		releaseList.add(new ReleaseTime("2015"));
		releaseList.add(new ReleaseTime("2016"));
		releaseList.add(new ReleaseTime("2017"));
		releaseList.add(new ReleaseTime("2018"));
		releaseList.add(new ReleaseTime("2019"));
		releaseList.add(new ReleaseTime("2020"));
	}
	
	private void initLevel(){
		levelList = new ArrayList<StartLevel>();
		levelList.add(new StartLevel("0"));
		levelList.add(new StartLevel("1"));
		levelList.add(new StartLevel("2"));
		levelList.add(new StartLevel("3"));
		levelList.add(new StartLevel("4"));
		levelList.add(new StartLevel("5"));
	}
	
	
	/**
	 * 初始化学生选课列表
	 * @param courseList
	 */
	private void initData(){
		StringBuffer buf = new StringBuffer();
		int i = 0;
		buf.append("<ul class='thumbnails'>");
		for(CourseBean b : courseList){
			CCPContants.getFilePath(b.getImage() , getFilePathService(),b.getFile_path_id() , CCPContants.getUserBean().getId() ,false,null);
			if(i % 4 == 0 && i != 0){
				buf.append("</ul>");
				buf.append("<ul class='thumbnails'>");
			}
			buf.append("<li class='span3'>");
			
			if(!b.getImage().equals("book-default.png")){
				String fileName = b.getImage().split("/")[1];
				b.setImage(CCPContants.getUserBean().getId()+"/"+fileName);
			}
			
			if(b.isHasSelected())
				buf.append("<img src='../../../tempfile/"+(b.getImage() == null ? "book-default.png'":b.getImage()) +"'  style=\"width:190px;height:156px;\"  alt='' >");
			else	
				buf.append("<a href='editSelect.faces?pageSize=8&showJoinStudyButton=true&onlineTeach=false&courseId="+b.getId()+"' class='img'><img src='../../../tempfile/"+(b.getImage() == null ? "book-default.png'":b.getImage()) +"'  style=\"width:190px;height:156px;\"  alt='' ></a>");
			buf.append("<div class='caption'>");
//			log.info("---------"+b.getCourse_name());
			buf.append("<p title=\""+b.getCourse_name()+"\">");
			buf.append(b.getCourse_name().length() > 9 ? b.getCourse_name().substring(0, 9)+ "...<br />" : b.getCourse_name()+"<br />");
			buf.append(getMessages("online_page_select_teacher")+b.getTeacherName()+"<br />");
			if(!b.getStart_level().equalsIgnoreCase("0"))
				buf.append("<span class='text-error'>"+b.getStart_level()+"</span>"+getMessages("online_page_select_star")+"<br />");
			else
				buf.append("<span class='text-error'></span>"+getMessages("online_page_select_star_level_no")+"<br />");
			buf.append("<span class='text-info'>"+b.getMarks()+"</span>"+getMessages("online_page_select_reply")+"<br />");
			buf.append("</p>");
			buf.append("</div>");
			if(b.isHasSelected())
				buf.append("&nbsp;&nbsp;<i class ='btn'>"+ getMessages("online_page_select_studing") +"</i>&nbsp;&nbsp;");	
			else
				buf.append("&nbsp;&nbsp;<a href='editSelect.faces?pageSize=8&showJoinStudyButton=true&onlineTeach=false&courseId="+b.getId()+"' class='btn btn-primary'>&nbsp;"+getMessages("online_page_select_action_browse")+"</a>&nbsp;&nbsp;");
			buf.append("<a href='../../../pages/course/studentCourseIssue.faces?id="+b.getId()+"' class='btn'>&nbsp;&nbsp;"+getMessages("online_page_select_action_quest")+"&nbsp;</a><br/>");
			buf.append("<span class='muted'>"+b.getStudyUsers()+getMessages("online_page_select_person_studing")+"</span>");
			buf.append("</li>");
			i++;
		}
		buf.append("</ul>");
		strCourseList = buf.toString();
//		log.info(strCourseList);
	}
	
	/**
	 * 专业变化
	 */
	public void specialtyChange(){
		log.info("-------------specialtyChange()---------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		selectSpecialtyId = varMap.get("specialtyId");
		log.info("selectSpecialtyId = "+selectSpecialtyId);
		getpagedetail();
	}
	
	/**
	 * 课程类型变化
	 */
	public void courseTypeChange(){
		log.info("-------------courseTypeChange()---------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		selectCourseType = varMap.get("selectCourseType");
		log.info("selectCourseType = "+selectCourseType);
		getpagedetail();
	}
	
	
	/**
	 * 课程语言类型变化
	 */
	public void courseLanguageChange(){
		log.info("-------------courseLanguageChange()---------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		selectLanguage = varMap.get("selectLanguage");
		getpagedetail();
	}
	
	
	public void userChange(){
		log.info("-------------userChange()---------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		selectTeacherID = varMap.get("teacherId");
		log.info("selectTeacherID = "+selectTeacherID);
		getpagedetail();
	}
	
	public void releaseTimeChange(){
		log.info("-------------releaseTimeChange()---------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		selectYear = varMap.get("releseTime");
		log.info("selectYear = "+selectYear);
		getpagedetail();
	}
	
	public void changeStartLevel(){
		log.info("-------------changeStartLevel()---------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		selectStarLevel = varMap.get("startLevel");
		log.info("selectStarLevel = "+selectStarLevel);
		getpagedetail();
	}
	
	
	public void doSearch(){
		log.info("----------"+strCourseName);
		getpagedetail();
	}
	
	 
	
	@Override
	public void getpagedetail() {
		setItemCount(getSelectCourseService().canSelectedCourse(CCPContants.getCurrUserId(),CCPContants.getUserBean().getGrade(),CCPContants.getUserBean().getSpecialtyId(),CCPContants.getUserBean().getFacultyId(),strCourseName,selectCourseType,selectTeacherID,selectStarLevel,selectYear,selectLanguage));
		int currentpage = getCurrentPage();
//		pageSize = 8;
		pageSize = Integer.parseInt(CCPContants.getValue("selectPageSize"));
		
		log.info("--pagesize = "+pageSize);
		setPageSize(pageSize);
		int start = (currentpage - 1) * pageSize;
		log.info("--strCourseName = "+strCourseName);
		log.info("--selectCourseType = "+selectCourseType);
		log.info("--selectTeacherID = "+selectTeacherID);
		log.info("--selectStarLevel = "+selectStarLevel);
		log.info("--selectYear = "+selectYear);
		log.info("--selectLanguage = "+selectLanguage);
		log.info("--currentpage = "+currentpage);
		log.info("--pagesize = "+pageSize);
		courseList = getSelectCourseService().canSelectedCourse(CCPContants.getCurrUserId(),CCPContants.getUserBean().getGrade(),CCPContants.getUserBean().getSpecialtyId(),CCPContants.getUserBean().getFacultyId(),start, pageSize,strCourseName,selectCourseType,selectTeacherID,selectStarLevel,selectYear,selectLanguage);
		if(pageSize == 8)
			initData();
	}

 



	public String getStrStudentId() {
		return strStudentId;
	}



	public void setStrStudentId(String strStudentId) {
		this.strStudentId = strStudentId;
	}

	public String getStrCourseList() {
		return strCourseList;
	}

	public void setStrCourseList(String strCourseList) {
		this.strCourseList = strCourseList;
	}

	public String getStrSpecialty() {
		return strSpecialty;
	}

	public void setStrSpecialty(String strSpecialty) {
		this.strSpecialty = strSpecialty;
	}

	public String getStrCourseName() {
		return strCourseName;
	}

	public void setStrCourseName(String strCourseName) {
		this.strCourseName = strCourseName;
	}

	public String getSelectSpecialtyId() {
		return selectSpecialtyId;
	}

	public void setSelectSpecialtyId(String selectSpecialtyId) {
		this.selectSpecialtyId = selectSpecialtyId;
	}

	public String getSelectTeacherID() {
		return selectTeacherID;
	}

	public void setSelectTeacherID(String selectTeacherID) {
		this.selectTeacherID = selectTeacherID;
	}

	public String getSelectStarLevel() {
		return selectStarLevel;
	}

	public void setSelectStarLevel(String selectStarLevel) {
		this.selectStarLevel = selectStarLevel;
	}

	public String getSelectYear() {
		return selectYear;
	}

	public void setSelectYear(String selectYear) {
		this.selectYear = selectYear;
	}

	public List<SpecialtyBean> getSpecailtyList() {
		return specailtyList;
	}

	public void setSpecailtyList(List<SpecialtyBean> specailtyList) {
		this.specailtyList = specailtyList;
	}

	public String getSelectLanguage() {
		return selectLanguage;
	}

	public void setSelectLanguage(String selectLanguage) {
		this.selectLanguage = selectLanguage;
	}

	public List<UserBean> getTeacherList() {
		return teacherList;
	}

	public List<ReleaseTime> getReleaseList() {
		return releaseList;
	}

	public void setReleaseList(List<ReleaseTime> releaseList) {
		this.releaseList = releaseList;
	}

	public void setTeacherList(List<UserBean> teacherList) {
		this.teacherList = teacherList;
	}
	 
	 public List<StartLevel> getLevelList() {
		return levelList;
	}

	public void setLevelList(List<StartLevel> levelList) {
		this.levelList = levelList;
	}

	public List<ResourseBean> getCourseTypeList() {
		return courseTypeList;
	}

	public List<ResourseBean> getLanguageList() {
		return languageList;
	}

	public void setLanguageList(List<ResourseBean> languageList) {
		this.languageList = languageList;
	}

	public String getSelectCourseType() {
		return selectCourseType;
	}

	public void setSelectCourseType(String selectCourseType) {
		this.selectCourseType = selectCourseType;
	}

	public List<CourseBean> getCourseList() {
		if(courseList == null)
			courseList = new ArrayList<CourseBean>();
		return courseList;
	}

	public void setCourseList(List<CourseBean> courseList) {
		this.courseList = courseList;
	}

	public String getBtnShowValue() {
		if(btnShowValue == null)
			btnShowValue = getMessages("label_list");	
		return btnShowValue;
	}

	public void setBtnShowValue(String btnShowValue) {
		this.btnShowValue = btnShowValue;
	}


	public class ReleaseTime{
		 private String release;
		 
		 public ReleaseTime(String release){
			 this.release = release;
		 }

		public String getRelease() {
			return release;
		}

		public void setRelease(String release) {
			this.release = release;
		}
		 
	 }
	 
	 public class StartLevel{
		 private String level;
		 private String des;
		 
		 public StartLevel(String level){
			 this.level = level;
			 this.des = level + getMessages("online_page_select_star");
		 }

		public String getLevel() {
			return level;
		}

		public void setLevel(String level) {
			this.level = level;
		}

		public String getDes() {
			return des;
		}

		public void setDes(String des) {
			this.des = des;
		}
		 
	 }
	
}
